package pl.szpadel.android.gadu;

import pl.szpadel.android.gadu.packets.ReceivedPacket;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

/// Manages user notification
public class UserNotification implements IAppComponent {
	
	private static final String TAG = "UserNotification";
	
	/// system notification manager
	private NotificationManager mNotificationManager = null;
	
	/// Id of persisten tniotification
	private int mOutgoingID = 1;
	
	/// If outgoing notification should be displayed
	private boolean mShowOutgoing = false;
	
	/// C-tor - initliazes with service
	public UserNotification(Context ctx) {
		mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
	}
	
	/// Shows persistent, outgoing notification
	public void showOutgoing() {
		mShowOutgoing = true;
		setStatusString(App.getInstance().getContext().getText(R.string.notification_disconnected));
	}
	
	/// Updates outgoing notification with status string
	private void setStatusString(CharSequence status) {
		// check if config allows for shoinwg outgoing notification
		if (App.getInstance().getConfig().getShowStatusbarIcon()) {
	    	Notification notification = new Notification(R.drawable.statusbar, "", 0);
	    	notification.flags = Notification.FLAG_ONGOING_EVENT;
	    	Context ctx = App.getInstance().getContext();
	    	
	    	CharSequence contentTitle =  ctx.getText(R.string.notification_app_name);
	    	CharSequence contentText = status;
	    	Intent notificationIntent = new Intent(ctx, ContactListActivity.class);
	    	PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
	    	notification.setLatestEventInfo(ctx, contentTitle, contentText, contentIntent);
	
	    	mNotificationManager.notify(mOutgoingID, notification);
		}
	}
	
	/// Hides outgoing message
	public void hideOutgoing() {
		mShowOutgoing = false;
		mNotificationManager.cancel(mOutgoingID);
	}
	
	@Override
	public void onPacket(ReceivedPacket packet) {
		// nothing
	}

	@Override
	public void onConnectionStateChanged(ConnectionState state) {
		AGLog.i(TAG, "Connection state changed, updating notification. state=" + state);
    	Context ctx = App.getInstance().getContext();

    	int resource = 0;
		switch(state.getState()) {
		case ConnectionState.CONNECTED:
			resource = R.string.notification_connected;
			break;
		case ConnectionState.CONNECTING:
			resource = R.string.notification_connecting;
			break;
		default:
			resource = R.string.notification_disconnected;
		}
		
		setStatusString(ctx.getText(resource));
	}
	
	/// Notifies about new message, with all the bells and whistles.
	void notifyNewMessage(String text, long sender, int unread) {
    	Notification notification = new Notification(R.drawable.statusbar_message, text, System.currentTimeMillis());
    	
    	// setup notification
    	Config cfg = App.getInstance().getConfig();

    	notification.defaults = 0;
		
		// sound
		String sound = cfg.getNotificationSound();
		if (sound.length() > 0){
			notification.sound = Uri.parse(sound);
		}
		
		// leds
		if (cfg.getUseLeds()) {
			notification.flags |= Notification.FLAG_SHOW_LIGHTS;
		}
		
		// vibration
		if (cfg.getUseVibration()) {
			notification.vibrate = new long[] {0, 300, 150, 300};
		}
    		
    	
    	
    	notification.number = unread;
    	
    	Context ctx = App.getInstance().getContext();
    	
    	String displayedName = App.getInstance().getContactInfoManager().uinToName(sender);
    	CharSequence contentTitle =  ctx.getText(R.string.msg_new_message) + " " + displayedName;
    	CharSequence contentText = text;
    	Intent notificationIntent = new Intent(ctx, ChatActivity.class);
    	notificationIntent.putExtra(ChatActivity.EXTRA_SENDER, sender);
    	
    	PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    	notification.setLatestEventInfo(ctx, contentTitle, contentText, contentIntent);

    	mNotificationManager.notify(2, notification);
	}
	
	/// silently notifies about unread messages
	/// Updates notification after one or more unread messages has been read
	void notifyUnread(String text, long sender, int unread) {
    	Notification notification = new Notification(R.drawable.statusbar_message, "", System.currentTimeMillis());
    	
    	notification.flags = Notification.FLAG_SHOW_LIGHTS;
    	notification.number = unread;
    	
    	Context ctx = App.getInstance().getContext();
    	
    	String displayedName = App.getInstance().getContactInfoManager().uinToName(sender);
    	CharSequence contentTitle =  ctx.getText(R.string.msg_new_message) + displayedName;
    	CharSequence contentText = text;
    	Intent notificationIntent = new Intent(ctx, ChatActivity.class);
    	notificationIntent.putExtra(ChatActivity.EXTRA_SENDER, sender);
    	
    	PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    	notification.setLatestEventInfo(ctx, contentTitle, contentText, contentIntent);

    	mNotificationManager.notify(2, notification);
	}
	
	/// Hides notification about underad messages
	void hideNotification() {
		mNotificationManager.cancel(2);
	}

	@Override
	public void onStatusChanged(Status status) {
		// TODO add to status string?
	}

	@Override
	public void onConfigChanged() {
		if (mShowOutgoing) {
			if (App.getInstance().getConfig().getShowStatusbarIcon()) {
				// show once again in case it was hidden previously
				onConnectionStateChanged(App.getInstance().getConnectionState());
			} else {
				// hide
				mNotificationManager.cancel(mOutgoingID);
			}
		}
	}
	
}
